package edu.ustb.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.mysql.cj.xdevapi.JsonArray;

import edu.ustb.domain.Goods;
import edu.ustb.service.GoodsService;
import edu.ustb.service.GoodsServiceImpl;

/**
 * Servlet implementation class GoodsListController
 */
@WebServlet("/goods/list.do")
public class GoodsListController extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		//用户需要提供当前的页数
		int newPageNum = 1; //默认第一页
		String pageNum = request.getParameter("pageNum");
		System.out.println("pageNum = " + pageNum);
		String target = request.getParameter("target");
		System.out.println("target = " + target);
		if(pageNum != null) {
			//证明用户第二次级之后进来，
			  newPageNum = Integer.parseInt(pageNum);
			  System.out.println("newPageNum = " + newPageNum);
		}
		
		try {
			GoodsService goodsService = new GoodsServiceImpl();
			
			PageInfo<Goods> pageInfo = goodsService.getPageGoodsBypageNum(newPageNum,target);
			System.out.println(JSON.toJSONString(pageInfo));
			response.getWriter().print(JSON.toJSONString(pageInfo));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
